/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.obj;

import java.io.Serializable;
import java.util.HashMap;

/**
 *
 * @author PHATNBSE61057
 */
public class Cart implements Serializable {

    private HashMap<String, Integer> items;
    private String bookID;

    public Cart() {
        this.items = new HashMap<String, Integer>();
        this.bookID = "00";
    }

    public Cart(String bookID) {
        this.items = new HashMap<String, Integer>();
        this.bookID = bookID;
    }

    public String getBookID() {
        return bookID;
    }

    public HashMap<String, Integer> getItems() {
        return items;
    }

    public boolean checkEmptyHashMap() {
        return items.isEmpty();
    }

    public void addItemToCart(String ID) {
        int quantity = 1;
        if (items.containsKey(ID)) {
            quantity = items.get(ID) + 1;
        }
        items.put(ID, quantity);
    }

    public void removeItemFormCart(String ID) {
        if (items.containsKey(ID)) {
            items.remove(ID);
        }
    }

    public boolean updateCart(String ID, int quantity) {
        if (items.containsKey(ID)) {
            if (quantity == 0) {
                items.remove(ID);
            } else {
                items.put(ID, quantity);
            }
            return true;
        }
        return false;
    }
}
